package com.jdjc.suject.domin.handler.subject.impl;

import com.jdjc.subject.common.enums.IsDeletedFlagEnum;
import com.jdjc.subject.common.enums.SubjectInfoTypeEnum;
import com.jdjc.subject.infra.basic.entity.SubjectJudge;
import com.jdjc.subject.infra.basic.service.SubjectJudgeService;
import com.jdjc.suject.domin.convert.JudgeSubjectConverter;
import com.jdjc.suject.domin.entity.SubjectAnswerBO;
import com.jdjc.suject.domin.entity.SubjectInfoBO;
import com.jdjc.suject.domin.entity.SubjectOptionBO;
import com.jdjc.suject.domin.handler.subject.SubjectTypeHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 判断题目的策略类
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectJudgeService subjectJudgeService;
    /**
     * 枚举身份的识别
     * @return
     */
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDUE;
    }

    /**
     * 实际题目的插入
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断题目的插入
        SubjectJudge subjectJudge = new SubjectJudge();
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(subjectJudge);


    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(Long.valueOf(subjectId));
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectJudge> result=subjectJudgeService.queryByConfition(subjectJudge);

        List<SubjectAnswerBO> subjectAnswerBOList= JudgeSubjectConverter.INSTANCE.convertJudgeListToBO(result);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);

        return subjectOptionBO;
    }
}
